package shopping.worker;

import com.jd.platform.async.callback.ICallback;
import com.jd.platform.async.callback.IWorker;
import com.jd.platform.async.worker.WorkResult;
import com.jd.platform.async.wrapper.WorkerWrapper;
import shopping.mode.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 满减
 */
public class ManJianWorker implements IWorker<TempOrder,Map<String,Object>> , ICallback<TempOrder,Map<String,Object>> {


    /**
     * 第一个被调用
     */
    @Override
    public void begin() {
        //ICallback.super.begin();
        /**
         * @TODO 初始化参数
         * 比如初始化需要得service服务对象
         */
        System.err.println("满减校验开始----------");
    }

    /**
     * 核心处理点
     * @param object      object  传入参数
     * @param allWrappers 任务包装  其他线程包裹对象
     * @return
     */
    @Override
    public Map<String,Object> action(TempOrder object, Map<String, WorkerWrapper> allWrappers) {
        // WorkerWrapper workerWrapper = allWrappers.get("one");  可以通过该对象获取其他得线程对象
        Map<String,Object> produce = (Map<String, Object>) allWrappers.get("produceWrapper").getWorkResult().getResult();
        Map<String,Object> reperty = (Map<String, Object>) allWrappers.get("repertyWrapper").getWorkResult().getResult();
        Map<String,Object> discount = (Map<String, Object>) allWrappers.get("discountCouponWorker").getWorkResult().getResult();

        Map<String,Object> result = null;

        /**
         * 产品结果处理
         */
        Set<String> pKeys = produce.keySet();
        for(String k : pKeys) {
            if(k != "success" && k != "msg") {
                Map<String,Object> rp = (Map<String,Object>)produce.get(k);
                if(!(boolean)rp.get("success")) {
                    result = rp;
                    break;
                }
            }
        }

        if(result != null && result.size() > 0) {
            return result;
        }

        /**
         * 库存
         */
        Set<String> repKeys = reperty.keySet();
        for(String rep : repKeys) {
            Map<String,Object> rp = (Map<String,Object>)produce.get(rep);
            if(!(boolean)rp.get("success")) {
                result = rp;
                break;
            }
        }

        if(result != null && result.size() > 0) {
            return result;
        }

        /**
         * 优惠券
         */
        if(!(boolean)discount.get("success")) {
            return discount;
        }

        /**
         * 创建订单头
         */
        Order order = new Order();
        order.setOrderCreated(new Date().toString());
        String orderNum = UUID.randomUUID().toString();
        order.setOrderNum(orderNum);
        order.setOrderId(orderNum);
        order.setUserId(object.getUser().getUserNo());

        List<Produce> ps = object.getProduces();

        List<OrderItem> orderItems = new ArrayList<>();

        BigDecimal bTotalPrice = new BigDecimal(0);
        for(Produce p : ps){
            OrderItem orderItem = new OrderItem();
            orderItem.setItemId(UUID.randomUUID().toString());
            orderItem.setOrderId(orderNum);
            orderItem.setPrice(p.getPrice());
            orderItem.setProdId(p.getProdNo());
            orderItem.setNum(p.getNum());
            BigDecimal price  = new BigDecimal(p.getPrice() * p.getNum());
            bTotalPrice = bTotalPrice.add(price);
            orderItems.add(orderItem);
        }
        order.setOrderItems(orderItems);
        // 设置总价格
        order.setTotalPrice(bTotalPrice.doubleValue());

        /**
         * 优惠券减免
         */
        DiscountCoupon discountCoupon = object.getDiscountCoupon();
        bTotalPrice = bTotalPrice.subtract(new BigDecimal(discountCoupon.getPrice()));

        /**
         * 如果优惠券见面后金额还是满100，然后在减30
         * 满一百减30
         */
        if(bTotalPrice.doubleValue() > 100) {
            bTotalPrice =bTotalPrice.subtract(new BigDecimal(30.00));
            order.setDiscountPrice(bTotalPrice.doubleValue());
        }
        if(result == null) {
            result = new HashMap<>();
        }
        result.put("success",true);
        result.put("order",order);
        return result;
    }

    @Override
    public Map<String,Object> defaultValue() {
        Map<String,Object> defaultResult = new HashMap<>();
        defaultResult.put("success",false);
        defaultResult.put("msg","满减校验异常，请联系管理员！");
        return defaultResult;
    }

    @Override
    public void result(boolean success, TempOrder param, WorkResult<Map<String,Object>> workResult) {
        Map<String,Object> result = (Map<String,Object>)workResult.getResult();
        boolean suFlag = (boolean)result.get("success");
        String msg = (String)result.get("msg");
        System.err.println("满减校验结束----------：" + success + ",msg:" + msg);
    }

}
